package com.magold.logic.bulb;

import org.andengine.entity.modifier.ColorModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.magold.logic.ISpriteLogic;
import com.magold.physics.MagoldPhysicsConnector;
import com.magold.sprite.IMultiTextureSprite;
import com.magold.sprite.MultiTextureSprite;

public class BulbBreaker {
	
	private static ITiledTextureRegion[] mBreakTextures;
	private static float[][] sBreakPieceCoor;	
	private static int sSize;
	
	private static Bulb.Constants sConstants;
	public static void customize(Bulb.Constants pConstants) {
		sConstants = pConstants;
		mBreakTextures = sConstants.mBreakTextures;
		sBreakPieceCoor = sConstants.mBreakPieceCoor;
		sSize = 7;
	}
	
// NO STATIC.	
	private BreakerWrapper[] mBreakSprites;
	private MagoldPhysicsConnector[] mConnectors;

	private final IEntityModifier mDarkenModifier;
	public BulbBreaker() {
		mDarkenModifier = new ColorModifier(0.5f, Color.YELLOW, Color.WHITE);
	}

	public MagoldPhysicsConnector findPhysicsConnector(IAreaShape pShape) {
		for(int i = 0; i < sSize; i++) {
			if(mBreakSprites[i].sameWith(pShape)) {
				return mConnectors[i];
			}
		}
		
		return null;
	}

	public void createSceneData(float pX, float pY, Scene pScene, ISpriteLogic pLogic) {		
		VertexBufferObjectManager vbom = sConstants.mContext.getVertexBufferObjectManager();
		
		mBreakSprites = new BreakerWrapper[sSize];
		for(int i = 0; i < sSize; i++) {
			float width = sBreakPieceCoor[i][2];
			float height = sBreakPieceCoor[i][3];
			float x = sBreakPieceCoor[i][0];
			float y = sBreakPieceCoor[i][1];
			
			IMultiTextureSprite rawSprite = new MultiTextureSprite(pX, pY, 0, mBreakTextures[i], vbom);
			rawSprite.setRotationCenter(x + width/2, y + height/2);
			pScene.attachChild(rawSprite);
			float[] coor = new float[] {x + pX, y + pY, width, height};
			mBreakSprites[i] = new BreakerWrapper(rawSprite, coor, pScene, pLogic, vbom);
			mBreakSprites[i].setVisible(false);
		}
	}

	public void createPhysicsData(PhysicsWorld pWorld) {
		BodyType bodyType = BodyType.StaticBody;
		FixtureDef def = sConstants.mBreakFixtureDef;
		
		mConnectors = new MagoldPhysicsConnector[sSize];
		
		for(int i = 0;i < sSize; i++) {
			mConnectors[i] = mBreakSprites[i].connectPhycicsWithBox(pWorld,bodyType, def);
		}
	}
		
	public void breakBulb(Vector2 pVelocity, boolean pIslightOn, PhysicsWorld pWorld) {
		BodyType type = BodyType.DynamicBody;
		for (int i = 0; i < sSize; i++) {
			Body body = mConnectors[i].getBody();
			body.setType(type);
			body.setLinearVelocity(pVelocity);
			
			mBreakSprites[i].setVisible(true);
			if(pIslightOn) mBreakSprites[i].registerEntityModifier(mDarkenModifier.deepCopy()); 
			mBreakSprites[i].registerSelfDestroy(this, sConstants.BREAKER_LIFE_CYCLE, pWorld, sConstants.mContext.getEngine());
		}
	}
}
